package com.lwj.kotlin.remotelibrary

import android.app.WallpaperManager
import android.content.*
import android.media.MediaPlayer
import android.service.wallpaper.WallpaperService
import android.view.SurfaceHolder
import java.io.IOException

class VideoLiveWallpaper : WallpaperService() {
    public var mVideoName = ""
    override fun onCreateEngine(): WallpaperService.Engine {
        return VideoEngine()
    }

    val VIDEO_PARAMS_CONTROL_ACTION = "com.zhy.livewallpaper"
    val KEY_ACTION = "action"
    val ACTION_VOICE_SILENCE = 110
    val ACTION_VOICE_NORMAL = 111

    fun voiceSilence(context: Context) {
        val intent = Intent(VIDEO_PARAMS_CONTROL_ACTION)
        intent.putExtra(KEY_ACTION, ACTION_VOICE_SILENCE)
        context.sendBroadcast(intent)
    }

    fun voiceNormal(context: Context) {
        val intent = Intent(VIDEO_PARAMS_CONTROL_ACTION)
        intent.putExtra(KEY_ACTION, ACTION_VOICE_NORMAL)
        context.sendBroadcast(intent)
    }

    fun setToWallPaper(context: Context) {
        val intent = Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
        intent.putExtra(
            WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
            ComponentName(context, VideoLiveWallpaper::class.java)
        )
        context.startActivity(intent)
    }


    internal inner class VideoEngine : WallpaperService.Engine() {

        private var mMediaPlayer: MediaPlayer? = null

        private var mVideoParamsControlReceiver: BroadcastReceiver? = null

        override fun onCreate(surfaceHolder: SurfaceHolder) {
            super.onCreate(surfaceHolder)
            Log.d("VideoEngine#onCreate")

            val intentFilter = IntentFilter(VIDEO_PARAMS_CONTROL_ACTION)
            mVideoParamsControlReceiver = object : BroadcastReceiver() {
                override fun onReceive(context: Context, intent: Intent) {
                    Log.d("onReceive")
                    val action = intent.getIntExtra(KEY_ACTION, -1)

                    when (action) {
                        ACTION_VOICE_NORMAL -> mMediaPlayer!!.setVolume(1.0f, 1.0f)
                        ACTION_VOICE_SILENCE -> mMediaPlayer!!.setVolume(0f, 0f)
                    }
                }
            }
            registerReceiver(mVideoParamsControlReceiver, intentFilter)

        }

        override fun onDestroy() {
            Log.d("VideoEngine#onDestroy")
            unregisterReceiver(mVideoParamsControlReceiver)
            super.onDestroy()

        }

        override fun onVisibilityChanged(visible: Boolean) {
            Log.d("VideoEngine#onVisibilityChanged visible = $visible")
            if (visible) {
                mMediaPlayer!!.start()
            } else {
                mMediaPlayer!!.pause()
            }
        }


        override fun onSurfaceCreated(holder: SurfaceHolder) {
            Log.d("VideoEngine#onSurfaceCreated ")
            super.onSurfaceCreated(holder)
            mMediaPlayer = MediaPlayer()
            mMediaPlayer!!.setSurface(holder.surface)
            try {
                if (Config.mVideoName.isEmpty()|| Config.mVideoName == "test1.mp4") {
                    val assetMg = applicationContext.assets
                    val fileDescriptor = assetMg.openFd("test1.mp4")
                    mMediaPlayer!!.setDataSource(
                        fileDescriptor.fileDescriptor,
                        fileDescriptor.startOffset, fileDescriptor.length
                    )
                }else{
                    print("视频路径"+Config.mVideoName)
                    mMediaPlayer!!.setDataSource(Config.mVideoName)
                }
                mMediaPlayer!!.isLooping = true
                mMediaPlayer!!.setVolume(0f, 0f)
                mMediaPlayer!!.prepare()
                mMediaPlayer!!.start()

            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        override fun onSurfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
            Log.d("VideoEngine#onSurfaceChanged ")
            super.onSurfaceChanged(holder, format, width, height)
        }

        override fun onSurfaceDestroyed(holder: SurfaceHolder) {
            Log.d("VideoEngine#onSurfaceDestroyed ")
            super.onSurfaceDestroyed(holder)
            mMediaPlayer!!.release()
            mMediaPlayer = null

        }
    }
}